home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr40 / x1j4pk96.zip / SETHELP.C < prev    next >
Text File  |  1995-03-11  |  10KB  |  280 lines

  1. /**************************************************************************
  2.  *
  3.  *  SETHELP - a program that changes the help text in the X1J part 2 binary
  4.  *
  5.  * (c) By: John Bednar WB3ESS
  6.  * This program is provided 'as is', with no warranty. It has been
  7.  * produced for use in self-tuition in amateur radio and is not
  8.  * to be used for commercial applications. It may be freely copied and
  9.  * used under those conditions.
  10.  *
  11.  *
  12.  * Invocation :
  13.  *      SETHELP bin_infile bin_outfile new_textfile
  14.  *      where: bin_infile   - the X1J binary file (part 2 only)
  15.  *             bin_outfile  - the name of the binary file to be created
  16.  *             new_textfile - the text file that contains the new help text
  17.  *
  18.  *     Create the text_file with your favorite editor and execute this
  19.  *     program. It will tell you how many bytes are remaining or how
  20.  *     many bytes too large your new help text is. If the starting
  21.  *     address of the help text changes in a future version, a #define
  22.  *     variable is provided to easily update this program.
  23.  *
  24.  *************************************************************************/
  25.  
  26. /*************************************************************************
  27.  * Return codes:
  28.  *
  29.  * The program exits with an error code of 0 if all went correctly.
  30.  * With 1 for a syntax error
  31.  *      2 for duplicate binary file name error
  32.  *      3 for an input file open error - original X1J file
  33.  *      4 for an output file open error - new X1J file
  34.  *      5 for an input file open error - help text file
  35.  *      6 for an input file read error - original X1J file
  36.  *      7 for an output file write error - new X1J file
  37.  *      8 for an file read or write error - reading/writing the help text
  38.  *      9 for a file that does not appear to be right version
  39.  *************************************************************************/
  40.  
  41. /*************************************************************************
  42.  *  Program revisions:
  43.  *  version 1.0 - 1/20/94: mailed to K3RLI and N3DPU
  44.  *  version 1.1 - 1/21/94: N3DPU found two variables that were not being
  45.  *              used. I corrected that and added a test to the dumpX1code()
  46.  *              to check the number of bytes that were written.
  47.  *  version 1.2 - 2/14/93: G8KBB minor changes, updated for TheNet X-1J
  48.  *              release 4 and renamed from HELPX1J. Added checksum routine
  49.  *
  50.  *************************************************************************/
  51.  
  52. #include <stdio.h>
  53.  
  54. #define UPTO 0x695e      /* help message starts with this byte  */
  55. #define ROMSIZE 0x7000    /* size of rom image segment space */
  56. #define HELPSPACE (ROMSIZE-UPTO)  /* space at end of segment for help text */
  57.  
  58. unsigned char Buffer[ROMSIZE+8];    /* buffer for i/p data from file */
  59. unsigned char *Ptr;             /* pointer into buffer for reading */
  60. FILE *Fpin, *Fpin2, *Fpout;     /* input & output file handles */
  61.  
  62.  
  63. main( argc, argv )
  64. char *argv[];
  65. {
  66.         int ecode=0;            /* error code on exit from main loop */
  67.  
  68.         printf("\n  X-1J release 4 [PK96] help text programmer by WB3ESS - Version 1.2\n");
  69.  
  70.         /* We need an input file, an output file, and an help text file.
  71.          *  4 command line parameters are required.
  72.          */
  73.         if( argc != 4 ) 
  74.         {
  75.                 printf( "Usage error, try:  sethelp bin_infile bin_outfile new_textfile\n");
  76.                 printf( "Where:  ");
  77.                 printf( "bin_infile   - the X1J binary file (part 2 only)\n");
  78.                 printf( "        bin_outfile  - the name of the binary file to be created\n");
  79.                 printf( "        new_textfile - the text file that contains the new help text\n");
  80.                 exit( 1 );
  81.         }
  82.  
  83.  
  84.         /*
  85.          *  Check to see if the binary filenames are different
  86.          */
  87.         if(!strcmp(argv[1],argv[2])) 
  88.         {
  89.                 printf("Error: binary filename names must be different.\n");
  90.                 exit( 2 );
  91.         }
  92.  
  93.  
  94.         /*
  95.          * We seem to have the parameters - so let's open the input binary
  96.          * file in binary mode.
  97.          */
  98.         if( ( Fpin = fopen( argv[1], "rb"  ) ) == NULL ) 
  99.         {
  100.                 printf("Error: X1J binary file can not be opened.\n");
  101.                 exit( 3 );
  102.         }
  103.  
  104.         /*
  105.          *  Next, open the output binary file for write in binary mode.
  106.          */
  107.         if( ( Fpout = fopen( argv[2], "wb" ) ) == NULL ) 
  108.         {
  109.                 printf("Error: binary file can not be opened.\n");
  110.                 exit( 4 );
  111.         }
  112.  
  113.         /*
  114.          *  Open the help text file for reading.
  115.          */
  116.         if( ( Fpin2 = fopen( argv[3], "rb"  ) ) == NULL ) 
  117.         {
  118.                 printf("Error: text file can not be opened.\n");
  119.                 exit( 5 );
  120.         }
  121.  
  122.  
  123.         /*
  124.          *  Now read the X1J binary file into memory
  125.          */
  126.         if( !readdata() )
  127.         {
  128.                 printf("Error: X1J input file size error or read error.\n");
  129.                 exit( 6 );
  130.         }
  131.  
  132.  
  133.         /*
  134.          *  Now check it is the right version
  135.          */
  136.         if( !checkdata() )
  137.         {
  138.                 printf("Error: File does not appear to be THENET2.X1J.\n");
  139.                 exit( 9 );
  140.         }
  141.  
  142.  
  143.         /*
  144.          *  Dump all the binary bytes up to (#UPTO) into the new file
  145.          */
  146.         if( !dumpX1code(UPTO)) 
  147.         {
  148.                 printf("Error: can not write the binary data into a file.\n");
  149.                 ecode = 7;
  150.         }
  151.  
  152.         /*
  153.          *  Read the new help text & write into the binary file
  154.          */
  155.         if( !dumptxt() )
  156.         {
  157.                 printf("Error: can not read help text or write into a file.\n");
  158.                 ecode = 8;
  159.         }
  160.  
  161.  
  162.         /*
  163.          *  close files and exit
  164.          */
  165.         fclose( Fpin );
  166.         fclose( Fpout );
  167.         fclose( Fpin2 );
  168.         return( ecode );
  169. }
  170.  
  171.  
  172.  
  173.  
  174. /**************************************************************************
  175.  * This function reads the entire X1 data into memory.
  176.  * A return of 0 means error, 1 means all went well.
  177.  **************************************************************************/
  178. readdata()
  179. {
  180.         unsigned int i;
  181.         i = fread( Buffer, sizeof(char), sizeof(Buffer), Fpin );
  182.         if( i < UPTO || i > ROMSIZE )
  183.                 return( 0 );
  184.  
  185.         if( ferror( Fpin ) )
  186.                 return( 0 );
  187.  
  188.         fclose( Fpin );
  189.         return( 1 );
  190. }
  191.  
  192.  
  193.  
  194. /***************************************************************************
  195.  * This function dumps the binary portion of the X1 data into the new
  196.  * binary file.
  197.  * A return of 0 means error, a return of 1 means all went well.
  198.  **************************************************************************/
  199. dumpX1code( num )
  200. unsigned int num;
  201. {
  202.         int i;
  203.         i = fwrite( Buffer, sizeof(char), num, Fpout );
  204.         if( i != num )
  205.                 return( 0 );
  206.         if( ferror(Fpout) )
  207.                 return( 0 );
  208.         return( 1 );
  209. }
  210.  
  211.  
  212.  
  213. /***************************************************************************
  214.  * This function reads the help text file, removes the '\n' characters,
  215.  * and dumps them into the end of the new binary file.
  216.  * It prints a message to the user telling how many bytes over or under
  217.  * you are.
  218.  * A return of 0 means error, a return of 1 means all went well.
  219.  **************************************************************************/
  220. dumptxt()
  221. {
  222.         unsigned char Textbuf[HELPSPACE+256];
  223.         unsigned char *Ptrtxt;          /* pointer to the output text buffer */
  224.         unsigned char *Ptrbuf;          /* pointer to the input text buffer */
  225.         unsigned int i,txtsize;
  226.  
  227.         txtsize = fread( Buffer, sizeof(char), sizeof(Buffer), Fpin2 );
  228.         if( ferror( Fpin2 ) )
  229.                 return( 0 );
  230.  
  231.         Ptrbuf = Buffer;        /*  set pointer to the beginning address */
  232.         Ptrtxt = Textbuf;       /*  set pointer to the beginning address */
  233.  
  234.         for(i=0; i<txtsize; i++)
  235.         {
  236.                 if ( *(Ptrbuf+i) != '\n' ) 
  237.                 {
  238.                         *Ptrtxt = *(Ptrbuf+i);   /* copy if not a newline  */
  239.                         Ptrtxt++;
  240.                 }
  241.         }
  242.  
  243.  
  244.         *Ptrtxt = '\0';         /*  terminate text file with a null  */
  245.         Ptrtxt++;               /*  needed to get the proper size  */
  246.  
  247.         if(Ptrtxt-Textbuf > HELPSPACE)
  248.         {
  249.                 printf("\n  ERROR: The binary output file is too large because the text file \n  contains too many characters.");
  250.                 printf(" You must remove %d byte(s) from the\n",(Ptrtxt-Textbuf)-HELPSPACE);
  251.                 printf("  input help text file and re-run this program.\n");
  252.         }
  253.         else
  254.                 printf("\n  You have %d byte(s) remaining for additional help text.\n", HELPSPACE-(Ptrtxt-Textbuf));
  255.  
  256.         fwrite( Textbuf, sizeof(char), (Ptrtxt-Textbuf), Fpout );
  257.         if( ferror(Fpout) )
  258.                 return( 0 );
  259.         return( 1 );
  260. }
  261.  
  262. /***************************************************************************
  263.  * This function does a rudimentary check for the right file version
  264.  * A return of 0 means error, a return of 1 means all went well.
  265.  **************************************************************************/
  266. checkdata()
  267. {
  268.     unsigned *ptr;
  269.     unsigned cksum = 0;
  270.     
  271.     for( ptr = Buffer+0x1080; ptr < Buffer+UPTO-2; ptr++ )
  272.         cksum += *ptr;
  273.  
  274. #ifdef DEBUG
  275.     fprintf( stderr, "Debug : checksum = 0x%04x\n", cksum );
  276. #endif
  277.     
  278.     return( cksum == 0xb54a );
  279. }
  280.